package cn.edu.cqyz.wisdom.service.impl;

import cn.edu.cqyz.wisdom.entity.Clock;
import cn.edu.cqyz.wisdom.entity.R;
import cn.edu.cqyz.wisdom.mapper.ClockMapper;
import cn.edu.cqyz.wisdom.service.IClockService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 打卡记录表 服务实现类
 * </p>
 *
 * @author CDHong.it
 * @since 2024-12-18
 */
@Service
public class ClockServiceImpl extends ServiceImpl<ClockMapper, Clock> implements IClockService {

    @Autowired
    public ClockMapper clockMapper;

    /**
     * 打卡
     */
    @Override
    public R clock(Integer userId) {
        QueryWrapper<Clock> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Clock> clockList = clockMapper.selectList(wrapper);
        LocalDate localDate = LocalDate.now(); // 获取当前年月日 并相加得到总值
        int time = localDate.getYear() + localDate.getMonthValue() + localDate.getDayOfMonth();
        // 如果第一次打开则添加数据
        if (clockList.isEmpty()) {
            Clock newClock = new Clock();
            newClock.setClockCount(1);
            newClock.setUserId(userId);
            newClock.setClockTime(time);
            try {
                clockMapper.insert(newClock);
                return R.ok("打卡成功!");
            } catch (Exception e) {
                return R.error("网络异常!");
            }
        } else {
            // 否则修改打卡天数
            if (time != clockList.get(0).getClockTime()) {
                clockList.get(0).setClockCount(clockList.get(0).getClockCount() + 1);
                clockList.get(0).setClockTime(time);
                try {
                    clockMapper.updateById(clockList.get(0));
                    return R.ok("打卡成功!");
                } catch (Exception e) {
                    return R.error("网络异常!");
                }
            } else {
                return R.error("今日已打卡!");
            }
        }
    }


    /**
     * 获取打卡状态 true 已打卡 false 未打卡
     */
    @Override
    public Boolean clockState(Integer userId) {
        LocalDate localDate = LocalDate.now(); // 获取当前年月日 并相加得到总值
        int time = localDate.getYear() + localDate.getMonthValue() + localDate.getDayOfMonth();
        QueryWrapper<Clock> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        int clockTime = clockMapper.selectOne(wrapper).getClockTime();
        return clockTime == time;
    }

    /**
     * 获取打卡天数
     * */
    @Override
    public Integer findClockCount(Integer userId){
        QueryWrapper<Clock> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Clock> clockList = clockMapper.selectList(wrapper);
        if (clockList.isEmpty()){
            return 0;
        }else {
            return clockList.get(0).getClockCount();
        }
    }

}
